﻿using System;
using System.IO;

namespace StockholmsStad.Utilities
{
    internal static class SVCWriter
    {
        private const string LANGUAGE = @"C#";
        private const string SERVICE_HOST = "ServiceHost";
        private const string PATH_EXTENSION = ".svc";
        private const string CONFIGURATION = "Debug";
        private const string DATA_CONTRACT = "DataContracts";

        internal static void GenerateFile(string outputFileDir, Type t, string configuration)
        {
            try
            {
                var fileName = Path.Combine(outputFileDir, Path.ChangeExtension(t.Name, PATH_EXTENSION));
                var fullClassName = string.Format("{0}.{1}", t.Namespace, t.Name);

                // Ignore datacontract classes
                if (!t.Namespace.Contains(DATA_CONTRACT))
                {
                    /// <%@ ServiceHost Language="C#" Debug="true" Service="StockholmsStad.ServiceModel.ServiceContracts.BookingService" %>
                    var serviceHost = string.Format("<%@ {0} Language=\"{1}\" {2}=\"{3}\" Service=\"{4}\" %>",
                        SERVICE_HOST, LANGUAGE, CONFIGURATION, configuration == "Debug" ? "true" : "false", fullClassName);

                    using (var writer = new StreamWriter(fileName))
                    {
                        writer.WriteLine(serviceHost);
                    }
                }
            }
            catch (Exception e)
            {
                Program.WriteWarning(e.Message.ToString());
            }
        }
    }
}
